<?php

/**
 * Application model for Cake.
 *
 * This file is application-wide model file. You can put all
 * application-wide model-related methods here.
 *
 * PHP 5
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.Model
 * @since         CakePHP(tm) v 0.2.9
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */
App::uses('Model', 'Model');

/**
 * Application model for Cake.
 *
 * Add your application-wide methods in the class below, your models
 * will inherit them.
 *
 * @package       app.Model
 */
class AppModel extends Model {

    public $actsAs = array('Containable');
    public $cacheQueries = true;

    public function beforeSave($options = array()) {
        parent::beforeSave($options);

        if (!empty($this->_schema['slug']) && empty($this->data[$this->name]['slug'])) {
            $nameField = (!empty($this->_schema['title']) ) ? 'title' : 'name';
            $currentSlug = null;
            if ($this->id) {
                $currentSlug = $this->field('slug');
            }
            if ($currentSlug == null) { // create slug
                $this->data[$this->name]['slug'] = $this->createSlug($this->data[$this->name][$nameField]);
            }
        }
        return true;
    }

    /**
     * Create table unique slug.
     * It's require table to have <b>slug</b> field.
     * If generated slug already have generated one 
     * this method create new one with counter eg:
     * <pre>
     *  "i-am-the-slug"
     *  if already exists it's create:
     *  "i-am-the-slug-1"
     * </pre>
     * 
     * @param String $name String from slug will be created
     * @param type $replacement passed to Inflector::slug method. Default "-"
     * @return String generated slug.
     *  <b>Never null</b> 
     * @link http://book.cakephp.org/sk/view/1479/Class-methods
     */
    protected function createSlug($name, $replacement = '-') {
        /**
         * Slug counter.
         * If generated slug exists increment value and insert at end of string and check again.
         */
        $no = 0;
        /**
         * Base slug. At end add counter (if != 0)
         */
        $slugBase = str_replace("_", $replacement, strtolower(Inflector::slug($name, $replacement)));
        /**
         * Slug to return.
         * It can't be null.
         */
        $slug = null;
        while (true) {
            $slug = $slugBase;
            if ($no != 0) {
                $slug .= '-' . $no;
            }
            $cnt = $this->find('count', array(
                'conditions' => array($this->name . '.slug' => $slug)
                    ));

            if ($cnt > 0) {
                $no++;
                continue;
            }
            break;
        }
        return $slug;
    }
}
